package com.huiztech.watersoil.utils;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.huiztech.watersoil.model.AreaMap;
import com.huiztech.watersoil.model.Stream;
/**
 * 公式转换器
 * 基于流域基本信息和用户输入的降雨值，计算出其他相关信息
 * @author alin
 *
 */
public class Convertor {

	private float x;
	private Stream stream;

	public String convertByCode(String code) {
		String result = "市区";
		AreaMap areaMap = new AreaMap();
		String firstThree = code.substring(0, 3);
		String firstFour = code.substring(0, 4);
		Iterator iterator = areaMap.getAreaMap().entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry entry = (Map.Entry) iterator.next();
			if (entry.getKey().equals(firstThree)) {
				result = entry.getValue().toString();
			} else if (entry.getKey().equals(firstFour)) {
				result = entry.getValue().toString();
			}
		}
		return result;
	}

	public Stream convertByX(float x, int factor_base_value, Stream stream) {

		// 按日计算
		if (factor_base_value == 0) {
			stream.setA(stream.getAr());
			stream.setB(stream.getBr());
		}
		// 按半月计算
		if (factor_base_value == 1) {
			stream.setA(stream.getAby());
			stream.setB(stream.getBby());
		}
		// 按月计算
		if (factor_base_value == 2) {
			stream.setA(stream.getAy());
			stream.setB(stream.getBy());
		}
		// 按季计算
		if (factor_base_value == 3) {
			stream.setA(stream.getAj());
			stream.setB(stream.getBj());
		}
		// 按年计算
		if (factor_base_value == 4) {
			stream.setA(stream.getAn());
			stream.setB(stream.getBn());
		}

		// 0现状侵蚀
		stream.setY0((float) (stream.getA() * Math.pow(x, stream.getB())));
		stream.setM0(stream.getY0() * stream.getS() / 10000);
		// 1梯田措施
		stream.setM1((float) (stream.getM0() - 0.905 * stream.getY0()
				* stream.getP1() / 10000));
		stream.setY1(stream.getM1() / stream.getS() * 10000);
		// 2横垄措施
		stream.setM2((float) (stream.getM0() - 0.417 * stream.getY0()
				* stream.getP1() / 10000));
		stream.setY2(stream.getM2() / stream.getS() * 10000);
		// 3免耕措施
		stream.setM3((float) (stream.getM0() - 0.91 * stream.getY0()
				* stream.getP1() / 10000));
		stream.setY3(stream.getM3() / stream.getS() * 10000);
		// 4秸秆覆盖措施
		stream.setM4((float) (stream.getM0() - 0.655 * stream.getY0()
				* stream.getP1() / 10000));
		stream.setY4(stream.getM4() / stream.getS() * 10000);
		// 5退耕还林
		stream.setM5((float) (stream.getM0() - 0.9 * stream.getY0()
				* stream.getP2() / 10000));
		stream.setY5(stream.getM5() / stream.getS() * 10000);
		// 6水土保持种草
		stream.setM6((float) (stream.getM0() - 0.9 * stream.getY0()
				* stream.getQ1() / 10000));
		stream.setY6(stream.getM6() / stream.getS() * 10000);
		// 7水土保持造林
		stream.setM7((float) (stream.getM0() - 0.938 * stream.getY0()
				* stream.getQ2() / 10000));
		stream.setY7(stream.getM7() / stream.getS() * 10000);
		// 8水平阶措施
		stream.setM8((float) (stream.getM0() - 0.905 * stream.getY0()
				* stream.getQ3() / 10000));
		stream.setY8(stream.getM8() / stream.getS() * 10000);
		// 9水平沟措施
		stream.setM9((float) (stream.getM0() - 0.938 * stream.getY0()
				* stream.getQ4() / 10000));
		stream.setY9(stream.getM9() / stream.getS() * 10000);
		// 10鱼鳞坑措施
		stream.setM10((float) (stream.getM0() - 0.9 * stream.getY0()
				* stream.getQ5() / 10000));
		stream.setY10(stream.getM10() / stream.getS() * 10000);

		return null;
	}

}
